package org.frog.sgs.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.frog.sgs.card.basic.Basic;
import org.frog.sgs.card.basic.type.Suit;
import org.frog.sgs.card.general.General;
import org.frog.sgs.card.role.Role;
import org.frog.sgs.server.ServerConfig;
import org.frog.sgs.util.SgsUtils;

/**
 * 卡牌管理器：所有静态数据
 * 
 * @author duanxiangqi
 * @date 2012-10-20
 */
@SuppressWarnings("unchecked")
public enum GameDataManager {
	instance;

	public static final String PATH_SGS_CONFIG_SIMPLE = "sgs-config-simple.xml";

	private static Logger logger = Logger.getLogger(GameDataManager.class);

	private List<Role> allRoles = new ArrayList<Role>();

	private List<General> allGenerals = new ArrayList<General>();

	private List<Basic> allBasics = new ArrayList<Basic>();

	private Map<Integer, ServerConfig> allGameConfigs = new LinkedHashMap<Integer, ServerConfig>();

	public void init() throws DocumentException, IOException {
		logger.info("加载配置文件：" + PATH_SGS_CONFIG_SIMPLE);
		Document document = SgsUtils.instance.loadXml(PATH_SGS_CONFIG_SIMPLE);
		Element root = document.getRootElement();
		parseRoles(root);
		parseGenerals(root);
		parseBasics(root);
		parseServerConfigs(root);
		logger.info("静态数据加载完毕");
	}

	private void parseRoles(Element root) {
		Iterator<Element> iterator = root.element("roles").elementIterator("role");
		List<Role> allRolesTemp = new ArrayList<Role>();
		while (iterator.hasNext()) {
			Element element = iterator.next();
			String idStr = element.attributeValue("id");
			String name = element.attributeValue("name");
			String classStr = element.attributeValue("class");
			String desc = element.attributeValue("desc");
			String enable = element.attributeValue("enable");
			if (!"1".equals(enable)) {
				continue;
			}
			try {
				Role role = (Role) Class.forName(classStr).newInstance();
				role.setId(Integer.valueOf(idStr));
				role.setName(name);
				role.setDesc(desc);
				allRolesTemp.add(role);
				logger.info("加载Role：" + role);
			} catch (Exception e) {
				logger.error("Role初始化出错：" + classStr, e);
			}
		}
		allRoles = allRolesTemp;
		logger.info("加载Role完毕：" + allRoles.size());
	}

	private void parseGenerals(Element root) {
		Iterator<Element> iterator = root.element("generals").elementIterator("general");
		List<General> temps = new ArrayList<General>();
		while (iterator.hasNext()) {
			Element element = iterator.next();
			String idStr = element.attributeValue("id");
			String name = element.attributeValue("name");
			String classStr = element.attributeValue("class");
			String desc = element.attributeValue("desc");
			String enable = element.attributeValue("enable");
			if (!"1".equals(enable)) {
				continue;
			}
			try {
				General general = (General) Class.forName(classStr).newInstance();
				general.setId(Integer.valueOf(idStr));
				general.setName(name);
				general.setDesc(desc);
				temps.add(general);
				logger.info("加载General：" + general);
			} catch (Exception e) {
				logger.error("General初始化出错：" + classStr, e);
			}
		}
		allGenerals = temps;
		logger.info("加载General完毕：" + allGenerals.size());
	}

	private void parseBasics(Element root) {
		Iterator<Element> iterator = root.element("basics").elementIterator("basic");
		List<Basic> temps = new ArrayList<Basic>();
		while (iterator.hasNext()) {
			Element element = iterator.next();
			String enable = element.attributeValue("enable");
			if (!"1".equals(enable)) {
				continue;
			}
			String idStr = element.attributeValue("id");
			String name = element.attributeValue("name");
			String pointsStr = element.attributeValue("points");
			String suitStr = element.attributeValue("suit");
			String classStr = element.attributeValue("class");
			String desc = element.attributeValue("desc");
			try {
				Basic basic = (Basic) Class.forName(classStr).newInstance();
				basic.setId(Integer.valueOf(idStr));
				basic.setName(name);
				basic.setPoints(Integer.valueOf(pointsStr));
				basic.setSuit(Suit.get(Integer.valueOf(suitStr)));
				basic.setDesc(desc);
				temps.add(basic);
				logger.info("加载Basic：" + basic);
			} catch (Exception e) {
				logger.error("Basic初始化出错：" + classStr, e);
			}
		}
		allBasics = temps;
		logger.info("加载Basic完毕：" + allBasics.size());
	}

	private void parseServerConfigs(Element root) {
		Iterator<Element> iterator = root.element("server-config").elementIterator("config");
		Map<Integer, ServerConfig> temps = new LinkedHashMap<Integer, ServerConfig>();
		while (iterator.hasNext()) {
			Element element = iterator.next();
			String enable = element.attributeValue("enable");
			if (!"1".equals(enable)) {
				continue;
			}
			String idStr = element.attributeValue("id");
			String playerNumStr = element.attributeValue("playerNum");
			String rolesStr = element.attributeValue("role-ref");
			String name = element.attributeValue("name");
			String desc = element.attributeValue("desc");
			try {
				ServerConfig config = new ServerConfig();
				config.setId(Integer.valueOf(idStr));
				config.setPlayerNum(Integer.valueOf(playerNumStr));
				String[] rolesArray = rolesStr.split(",");
				int[] roles = new int[rolesArray.length];
				for (int i = 0; i < roles.length; i++) {
					roles[i] = Integer.valueOf(rolesArray[i]);
				}
				config.setRoles(roles);
				config.setName(name);
				config.setDesc(desc);
				temps.put(config.getId(), config);
				logger.info("加载GameConfig：" + config);
			} catch (Exception e) {
				logger.error("GameConfig初始化出错：" + element, e);
			}
		}
		allGameConfigs = temps;
		logger.info("加载GameConfig完毕：" + allGameConfigs.size());
	}

	public Role getRoleCopy(int id) {
		for (Role role : allRoles) {
			if (role.getId() == id) {
				return role;
			}
		}
		return null;
	}

	public General getGeneralCopy(int id) {
		for (General general : allGenerals) {
			if (general.getId() == id) {
				return general;
			}
		}
		return null;
	}

	public Basic getBasicCopy(int id) {
		for (Basic basic : allBasics) {
			if (basic.getId() == id) {
				return basic;
			}
		}
		return null;
	}

	public List<Role> getAllRolesCopy() {
		return SgsUtils.instance.copy(allRoles);
	}

	public List<General> getAllGeneralsCopy() {
		return SgsUtils.instance.copy(allGenerals);
	}

	public List<Basic> getAllBasicsCopy() {
		return SgsUtils.instance.copy(allBasics);
	}

	public Map<Integer, ServerConfig> getAllGameConfigs() {
		return allGameConfigs;
	}

	public List<Role> getAllRoles() {
		return allRoles;
	}

	public List<General> getAllGenerals() {
		return allGenerals;
	}

	public List<Basic> getAllBasics() {
		return allBasics;
	}
}
